﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DingTalkAgent.Web.Controllers
{
    // [Common.Filter.AuthorizeFilter]
    public class OperateController : Common.adminBaseController
    {
        //获取列表信息
        public ActionResult List(int PageIndex = 1)
        {
            try
            {
                if (Request.IsAjaxRequest())
                {
                    int rows = Convert.ToInt32(Request["rows"].ToString());
                    int page = Convert.ToInt32(Request["page"].ToString());
                    var keyword = Request["keyword"].ToString();
                    int total = 0;
                    var dh = new SqlData.Operate();
                    List<DingTalkAgent.Model.Operate> list = dh.GetPageList(page, rows, out total, keyword, "");
                 
                    return Json(list);
                }
                return View();
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/Operate/List", ex.Message, _view_Admin_Info.Name, "");
                return Content(ConfigHelper.getErrorMsg);
            }
        }

        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult Add(Model.Operate model)
        {
            try
            {
                var dh = new SqlData.Operate();
                if (dh.Add(model) > 0)
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "", ":", _view_Admin_Info.Name);
                    return Content("true");
                }
                else
                {
                    return Content("保存信息失败!");
                }
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/Operate/Add", ex.Message, _view_Admin_Info.Name);
                return View();
            }
        }

        /// <summary>
        /// 详细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult details(int id)
        {
            try
            {
                var dh = new SqlData.Operate();
                var m = dh.GetModel(id);
                return View(m);
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/Operate/Edit", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }

        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(Model.Operate model)
        {
            try
            {
                var dh = new SqlData.Operate();
                if (dh.Update(model))
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "", ":", _view_Admin_Info.Name);
                    return Content("true");
                }
                else
                {
                    return Content("保存信息失败！");
                }
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/Operate/Edit", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }
        /// <summary>
        /// 删除单个信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            try
            {
                var dh = new SqlData.Operate();
                if (dh.Delete(id))
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "", ":id:" + id, _view_Admin_Info.Name);
                    return Content("true");
                }
                else
                {
                    return Content("删除信息失败!");
                }
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/Roles/Delete", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Deletes(string ids)
        {
            try
            {
                //
                var dh = new SqlData.Operate();
                ids = Discuz.Common.Utils.ChkSQL(ids).TrimEnd(',');
                if (dh.Deletes(ids))
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "批量删除企业", "成功批量删除企业:ids:" + ids, _view_Admin_Info.Name);
                    return Content("true");
                }
                else
                {
                    return Content("删除信息失败!");
                }
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/Roles/Deletes", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }

        /// <summary>
        /// 导出信息-指定查询的
        /// </summary>
        /// <returns></returns>
        public ActionResult export()
        {
            try
            {
                //条件
                int rows = Convert.ToInt32(Request["rows"].ToString());
                int page = Convert.ToInt32(Request["page"].ToString());
                var keyword = Request["keyword"].ToString();

                //要导出的字段和期对应的导出后的名称


                var dh = new SqlData.Operate();
                return Content("true");
              
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/Operate/Delete", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult prints(int id)
        {
            try
            {
                var dh = new SqlData.Operate();
                var m = dh.GetModel(id);
                return View();
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/Operate/prints", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }

    }
}